//
//  DBJAttentionDoctorTableViewController.m
//  DBJ
//
//  Created by 花化华 on 2016/11/12.
//  Copyright © 2016年 itcast. All rights reserved.
//
#define DOCCELL @"AttDocCell"
#import "DBJAttentionDoctorTableViewController.h"
#import "DBJDoctorListForAttentionCell.h"
#import <MJRefresh.h>
#import "DBJDoctorDetailViewController.h"
#import "DBJDocListModel.h"
#import "DBJDocDetailModel.h"
#import "DBJApplyforDoctorViewController.h"
#import <SVProgressHUD.h>
@interface DBJAttentionDoctorTableViewController ()

/**
 医生列表模型数组
 */
@property(strong,nonatomic)NSArray<DBJDocListModel *> *docList;

/**
 医生详情模型数组
 */
@property(strong,nonatomic)NSArray<DBJDocDetailModel *> *details;

/**
 医生简介模型数组
 */
@property(strong,nonatomic)NSArray<DBJDocIntrodunceModel *> *intros;

/**
 医生接诊条件模型数组
 */
@property(strong,nonatomic)NSArray<DBJCureConditionModel *> *conditions;
@end

@implementation DBJAttentionDoctorTableViewController

-(NSArray *)docList{
    if (_docList == nil) {
        _docList = [NSArray array];
    }
    return _docList;
}
- (void)dbjSetupNavigationItems{
    [super dbjSetupNavigationItems];
}
-(instancetype)init{
    if (self =[super init]) {
        [DBJTools dbjShowSVTitle:@"玩命加载中...." andTime:1.0];
    }
    return self;
}

- (void)dbjSetupSubviews{
    [super dbjSetupSubviews];
    
        /*------------*1. 搭建UI *-------------*/
    //注册cell
    UINib *nib = [UINib nibWithNibName:@"DBJDoctorListForAttentionCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:DOCCELL];
    //设置行高
    self.tableView.rowHeight = 80;
    
    /*------------*2. 设置上下拉刷新 *-------------*/
    __weak typeof(self) weakSelf = self;
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //刷新指定时间完成
        [NSThread sleepForTimeInterval:2.0];
        [weakSelf.tableView.mj_header endRefreshing];
        [DBJTools dbjShowSVSuccessTitle:@"加载完成" andTime:0.8];
    }];
    //上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //刷新指定时间完成
        [NSThread sleepForTimeInterval:2.0];
        [weakSelf.tableView.mj_footer endRefreshing];
        [DBJTools dbjShowSVSuccessTitle:@"加载完成" andTime:0.8];
        //[self refreshUpOrDown];
    }];
    //加载数据
    [self loadData];
}
/**
 刷新完成方法
 */
//-(void)refreshUpOrDown{
//    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
//    [SVProgressHUD setBackgroundColor:DBJColor];
//    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
//    [SVProgressHUD setMinimumDismissTimeInterval:1.0];
//    [SVProgressHUD showSuccessWithStatus:@"刷新完成"];
//}
#pragma mark - 获取网络数据

-(void)loadData{
    /*------------* 获取医生列表数据 *-------------*/
    NSString *url;
    NSDictionary *parameter;
    if (![self.title isEqualToString:@"关注医生"]) {
        url = @"http://iosapi.itcast.cn/doctor/matchDoctors.json.php";
        parameter = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"page_size":@15,@"is_confirmed":@1,@"user_id":@1000089, @"page":@1,@"has_diagnosis":@2};
    }else
    {
            url = @"http://iosapi.itcast.cn/doctorList.json.php";
            parameter = @{@"user_id":@1000089,@"page_size":@15,@"page":@1};
    }
    


    [DBJDocListModel getDoctorListDataWithURLString:url andParameters:parameter andSuccessBlock:^(NSArray *newsList) {
        self.docList = newsList;
        [self.tableView reloadData];
    } andErrorBlock:^(NSError *error) {
        NSLog(@"error = %@",error);
    }];
    
        /*------------* 获取医生接诊条件数据 *-------------*/
    NSString *conditionURL = @"http://iosapi.itcast.cn/doctor/doctorReceivingSetting.json.php";
    NSDictionary *conditionParameter = @{@"doctor_id":@300000315};
    [DBJCureConditionModel getDoctorListDataWithURLString:conditionURL andParameters:conditionParameter andSuccessBlock:^(NSArray *newsList) {
        self.conditions = newsList;
    } andErrorBlock:^(NSError *error) {
        NSLog(@"error = %@",error);
    }];
    
        /*------------* 获取医生简介数据 *-------------*/
    NSString *introURL = @"http://iosapi.itcast.cn/doctor/getIntroduction.json.php";
    NSDictionary *introsParameter = @{@"doctor_id":@300000315};
    [DBJDocIntrodunceModel getDoctorListDataWithURLString:introURL andParameters:introsParameter andSuccessBlock:^(NSArray *newsList) {
        self.intros = newsList;
    } andErrorBlock:^(NSError *error) {
        NSLog(@"error = %@",error);
    }];
    
    
    }

#pragma mark - 数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.docList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //1.创建cell
    DBJDoctorListForAttentionCell *cell = [tableView dequeueReusableCellWithIdentifier:DOCCELL forIndexPath:indexPath];
    //2.给cell赋值
    cell.docModel = self.docList[indexPath.row];
    //3.返回cell
    return cell;
}

#pragma mark - 代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //创建详情页控制器
    DBJDoctorDetailViewController *detail = [[DBJDoctorDetailViewController alloc]init];
    if (![self.title isEqualToString:@"关注医生"]) {
        DBJApplyforDoctorViewController *applyDoc = [[DBJApplyforDoctorViewController alloc]init];
        NSString *url = @"http://iosapi.itcast.cn/doctor/matchDoctors.json.php";
        NSDictionary *parameter = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"page_size":@15,@"is_confirmed":@1,@"user_id":@1000089, @"page":@1,@"has_diagnosis":@2};
        [DBJDocListModel getDoctorListDataWithURLString:url andParameters:parameter andSuccessBlock:^(NSArray *newsList) {
           applyDoc.docLists = newsList[indexPath.row];
            applyDoc.introduceModel = self.intros.lastObject;
            applyDoc.conditionModel = self.conditions.lastObject;
        } andErrorBlock:^(NSError *error) {
            NSLog(@"error = %@",error);
        }];
        [self.navigationController pushViewController:applyDoc animated:YES];
        return;
    }
    
    /*------------* 点击之后跳转进行数据刷新 *-------------*/
//    NSString *url = @"http://iosapi.itcast.cn/doctor/matchDoctors.json.php";
//    NSDictionary *parameter = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"page_size":@15,@"is_confirmed":@1,@"user_id":@1000089, @"page":@1,@"has_diagnosis":@2};
    NSString *url = @"http://iosapi.itcast.cn/doctorList.json.php";
    NSDictionary *parameter = @{@"user_id":@1000089,@"page_size":@15,@"page":@1};
    [DBJDocListModel getDoctorListDataWithURLString:url andParameters:parameter andSuccessBlock:^(NSArray *newsList) {
        detail.detailModel = newsList[indexPath.row];
        detail.introduceModel = self.intros.lastObject;
        detail.conditionModel = self.conditions.lastObject;
    } andErrorBlock:^(NSError *error) {
        NSLog(@"error = %@",error);
    }];
    //点击进行push.
    [self.navigationController pushViewController:detail animated:YES];
    //设置返回按钮的文字
    self.navigationItem.backBarButtonItem.title = @"关注医生";
       }

@end
